<template>
<div class="jjmap">
  <div class="city_list">
    <div v-for="(item,index) in cityList" :key="index" @click="changeCity(index,item)">{{item}}</div>
  </div>
	<div id="jjmap" ref="mapChart"></div>
</div>
</template>
<script>
import chinaMap from "@/utils/china.json";
import {  chain_city_map , getMap , city } from "@/api/api.js";
import mapAll from "@/utils/mapAll.json";
import { option4 as option1 } from "@/utils/mapData.js";
var convertData = function (data) {
  var res = []; 
  for (var i = 0; i < data.length; i++) {
    res.push({
      name: data[i].city_name.split('省')[0],
      value: [data[i].lng, data[i].lat, data[i].total],
    });
  }
  return res; 
};
var myChart = null; 

export default {
  data(){
    return{
      chainList:[],
      chain_id:'',
      isLoading:false,
      isloading:false,
      city_name:'',

      shiList:[],
      quList:[],
      shengList:[],
      sheng_id:'',
      shi_id:'',
      qu_id:'',
      city_id:'',
      cityItem:{city_name:'贵州'},
      cityList:['贵州'],
      chainItem:{},
    }
  },
	components:{
		
	},
  created(){
    this.getCity()
  },
  mounted(){
    this.cityItem={city_name:localStorage.myCity_name,type:localStorage.myCity_type}
    this.cityList=[localStorage.myCity_name]
    this.city_id = localStorage.myCity_id
    if(localStorage.myCity_type==1){
      this.sheng_id = localStorage.myCity_id
    }else if(localStorage.myCity_type==2){
      this.shi_id = localStorage.myCity_id
    }else if(localStorage.myCity_type==3){
      this.qu_id = localStorage.myCity_id
    }
		myChart = this.$echarts.init(document.getElementById("jjmap"));
    myChart.on("click", (param)=>{
      this.changeMap(param.name)
    });
    this.getMap({city_name:localStorage.myCity_name})
  },
  beforeDestroy(){
    if(myChart){
      myChart.clear()
    }
  },
  methods:{
		getData(chain_id){
			this.chainItem.cityItem=this.cityItem
			this.chainItem.chain_id = chain_id
			this.getLine(this.chainItem)
		},
    getCity(){
      city({city_id:localStorage.myCity_id}).then(res=>{
        if(localStorage.myCity_type==1){
          this.shiList=res.data
        }else if(localStorage.myCity_type==2){
          this.quList=res.data
        }
      })
    },
		chainAndCity(){
			this.chainItem.cityItem = this.cityItem
			this.getLine(this.chainItem)
			this.cityItem.shengShow = false
      if(localStorage.myCity_type==1){
			  this.cityItem.shiShow = this.cityList.length==1?true:false
			  this.cityItem.quShow = this.cityList.length<=2?true:false
      }else if(localStorage.myCity_type==2){
			  this.cityItem.shiShow = false
			  this.cityItem.quShow = this.cityList.length==1?true:false
      }else {
        this.cityItem.shiShow = false
			  this.cityItem.quShow = false
      }
			this.$emit('changeCity',this.cityItem)
		},
    shengChange(id,city_name=''){
      this.sheng_id=id
      this.qu_id = ''
      this.shi_id = ''
      this.quList=[]
			this.city_id=id
			this.cityItem = {city_name}
      this.cityList =[city_name] 
      this.$forceUpdate();
			this.chainAndCity()
    },
    shiChange(id){
      this.shi_id=id
      this.qu_id = ''
      this.quList=[]
      let index= this.shiList.findIndex(item=>item.id==id)
      if(index!=-1){
        this.quList = this.shiList[index].children
        this.cityItem = this.shiList[index]
        if(localStorage.myCity_type==1){
          this.cityList[1]=this.cityItem.city_name
        }else if(localStorage.myCity_type==2){
          this.cityList[0]=this.cityItem.city_name
        }
        this.$forceUpdate();
        this.city_id=id
        this.chainAndCity()
      }else{
        this.cityItem = this.shengList.find(item=>item.id==this.sheng_id)
        this.city_id= this.sheng_id||1
        this.chainAndCity()
      }
    },
    changeCity(index,item){
			this.cityList.splice(index+1,2-index)
      this.$forceUpdate();
      if(index==0){
        if(localStorage.myCity_type==1){
          this.shengChange(localStorage.myCity_id,localStorage.myCity_name)
        }
      }else{
        this.changeMap(item,index)
      }
    },
    changeMap(city_name,index){
      if(index==1){
        let item_city =this.shengList.find(item=>(item.city_name.indexOf(city_name)==0||city_name.indexOf(item.city_name)==0))
        if(item_city){
          this.shengChange(item_city.id,city_name)
        }
      }
      city_name=city_name.split("县")[0].split("区")[0]
      if(this.sheng_id){
        let item_city =this.shiList.find(item=>(item.city_name.indexOf(city_name)==0||city_name.indexOf(item.city_name)==0))
        if(item_city){
          this.shiChange(item_city.id)
        }
      }else{
        let item_city =this.shengList.find(item=>(item.city_name.indexOf(city_name)==0||city_name.indexOf(item.city_name)==0))
        if(item_city){
          this.shengChange(item_city.id,city_name)
        }
      }
    },



    getLine(item){
      console.log(item)
      if(!item.cityItem.city_name){
				this.$echarts.registerMap("china", chinaMap);
				option1.series[0].map = "china";
				if(myChart){
					this.$echarts.registerMap("china", chinaMap);
					option1.series[0].map = "china";
					myChart.setOption(option1);
				}
      }else{
        // this.city_name = item.cityItem.city_name
        this.getMap(item.cityItem)
      }
      this.isLoading=true
      chain_city_map({city_id:item.cityItem.id||this.city_id,chain_id:this.chainItem.chain_id||'104569'}).then(res=>{
        this.lineList=res.data
				console.log(res.data)
        this.draw(this.lineList)
        this.isLoading=false
      })
    },
    getMap(cityItem){
      let city_code1 = mapAll.find(
        (item) =>
          cityItem.city_name.indexOf(item.name) == 0 ||
          item.name.indexOf(cityItem.city_name) == 0
      )
      if(!city_code1){
        this.isloading=false
        return
      }
      let city_code=city_code1.code
      getMap({ code: city_code, type: (cityItem.type != 3) ? "_full" : "" }).then(
        ({ data }) => {
          myChart.clear();
          this.$echarts.registerMap(city_code, data);
          option1.series[0].map = city_code;
          if (cityItem.type == 3) {
          } else {
          }
          myChart.setOption(option1, true);
          this.isloading = false;
        }
      ).catch(err=>{
        this.isloading=false
      })
    },
    draw(list){
      this.xData=[]
      this.yData=[]
      if(!list){
        return
      }
      option1.series[0].data = convertData(list)
      let list1 = list.sort(function(a, b) {
        return b.total- a.total;
       }).slice(0, 10)

      if(list1[0].total==0){
        option1.visualMap.max=1
      }else{
        option1.visualMap.max = list1[0].total
      }
      myChart.setOption(option1,true)
    }
  }
}
</script>
<style lang="scss" scoped>
.jjmap{
  height: 350px;
  width: calc(100% - 10px);
  margin: 5px;
  border: 2px solid #ccc;
  border-radius: 5px;
  background: #f3f4fb;
  .header{
    margin: 5px;
    font-size: 18px;
    font-weight: 600;
  }
  position: relative;
}
#jjmap{
  width: 100%;
  height: calc(100% - 40px);
}
.city_list{
  display: flex;
  position: absolute;
  right: 10px;
  top: 5px;
  font-size: 16px;
	z-index:1000;
  div{
    margin: 0 5px;
    font-weight: 600;
    color: rgb(8, 154, 252);
  }
}
</style>